﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace UnitySheet
{
    public class SheetUI : MonoBehaviour
    {
        public Button createSheet, deleteSheet;
        //public Button saveSheet, readSheet;
        public Button addRow, addColumn;
        public Button deleteRow, deleteColumn;
        public Button splite, spliteColumn, merge;
        public InputField columnNum, rowNum;
        // Use this for initialization
        void Start()
        {
            createSheet.onClick.AddListener(delegate () { SheetManager.sheetManager.CreateSheet(); });
            deleteSheet.onClick.AddListener(SheetManager.sheetManager.DeleteSheet);

            addRow.onClick.AddListener(SheetManager.sheetManager.sheetOperator.AddRow);
            addColumn.onClick.AddListener(SheetManager.sheetManager.sheetOperator.AddColumn);
            merge.onClick.AddListener(SheetManager.sheetManager.sheetOperator.Merge);
            splite.onClick.AddListener(SheetManager.sheetManager.sheetOperator.SpliteRow);
            spliteColumn.onClick.AddListener(SheetManager.sheetManager.sheetOperator.SpliteColumn);

            deleteRow.onClick.AddListener(SheetManager.sheetManager.sheetOperator.DeleteRowCells);
            deleteColumn.onClick.AddListener(SheetManager.sheetManager.sheetOperator.DeleteColumnCells);
            SheetManager.sheetManager.onSelectOneSheet += OnSelectOneSheet;
            SheetManager.sheetManager.onDeselectSheet += OnDeselectOneSheet;
        }

        // Update is called once per frame
        void Update()
        {

        }

        void OnSelectOneSheet(Sheet sheet)
        {
            deleteSheet.interactable = true;

            addRow.interactable = true;
            addColumn.interactable = true;
            merge.interactable = true;
            splite.interactable = true;
            spliteColumn.interactable = true;

            columnNum.interactable = true;
            rowNum.interactable = true;

            deleteRow.interactable = true;
            deleteColumn.interactable = true;
        }

        void OnDeselectOneSheet(Sheet sheet)
        {
            if (SheetManager.sheetManager.selectedSheet == null)
            {
                deleteSheet.interactable = false;

                addRow.interactable = false;
                addColumn.interactable = false;
                merge.interactable = false;
                splite.interactable = false;
                spliteColumn.interactable = false;

                columnNum.interactable = false;
                rowNum.interactable = false;

                deleteRow.interactable = false;
                deleteColumn.interactable = false;
            }
        }
    }
}
